//
//  HomeViewController.m
//  项目1 CLCMovie
//
//  Created by huiwenjiaoyu on 15/7/17.
//  Copyright (c) 2015年 CLC. All rights reserved.
//

#import "HomeViewController.h"
#define kWidth  self.view.bounds.size.width
#define kHieght self.view.bounds.size.height


@interface HomeViewController ()

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];

    [self _creatNavigationItem];
    [self _creatTableView];
    [self _creatPosterView];
    [self.navigationItem setTitle:@"首页"];
    
//    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
}


//创建navigation
- (void)_creatNavigationItem
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [view setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:view];
    
    
    UIImage* img = [UIImage imageNamed:@"list_home"];
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:img forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 55, 30);
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    
    UIBarButtonItem* btn1 = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = btn1;
    _isListPage = YES;
    
    
    
//    UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list_home"] style:UIBarButtonItemStylePlain target:self action:@selector(btnAction)];
//
//    [btn setTintColor:[UIColor whiteColor]];
//    [btn setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//    //按钮相对于Bar的中线垂直位置调整，正数为向下偏移
//    //   [btn setBackgroundVerticalPositionAdjustment:15 forBarMetrics:UIBarMetricsDefault];
//    [btn setWidth:0];
//
//    self.navigationItem.rightBarButtonItems = @[btn];
    
}


#pragma mark - 按钮/页面翻转


- (void)btnAction:(UIButton*)btn
{
    UIView* flipView = self.navigationItem.rightBarButtonItem.customView;
    //home页翻转效果
    [self flipView:self.view];
    [self flipView:flipView];

    if(_isListPage)
    {
        [btn setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
        _posterView.hidden = NO;
        _MovieTableView.hidden = YES;
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
        _MovieTableView.hidden = NO;
        _posterView.hidden = YES;
    }
    
    
    _isListPage = !_isListPage;
}

//翻转动画封装
- (void)flipView:(UIView*)view
{
    UIViewAnimationOptions option = _isListPage?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    
    
    //翻转指令语句
    [UIView transitionWithView:view duration:.3 options:option animations:nil completion:nil];
}

#pragma mark - TableView创建

//创建tableview
- (void)_creatTableView
{
    _MovieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHieght-108) style:UITableViewStylePlain];
    _MovieTableView.delegate = self;
    _MovieTableView.dataSource = self;

    UINib* cellNib = [UINib nibWithNibName:@"HomeCellView" bundle:[NSBundle mainBundle]];
    
    [_MovieTableView registerNib:cellNib forCellReuseIdentifier:@"movieCell"];
    _MovieTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_MovieTableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [modalArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 112;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell" forIndexPath:indexPath];

    cell.homeModal = modalArray[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 海报界面

- (void)_creatPosterView
{
    _posterView = [[PostView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHieght-108)];
    [self.view addSubview:_posterView];
    _posterView.hidden = YES;
    
    _posterView.modalArray = [NSArray arrayWithArray:modalArray];

}



#pragma mark - 数据加载

- (void)_loadData
{
    //读取json数据
    NSDictionary* dic = [DataService getArrayOrDictionNaryfromJosnWithNamed:@"us_box"];
    //创建数组纪录每个Cell的modal数据
    modalArray = [[NSMutableArray alloc] init];
    
    //
    NSArray* subArray = [NSArray arrayWithArray:[dic valueForKey:@"subjects"]];
    

    for(int i = 0;i < [subArray count];i++)
    {

        NSDictionary* dic1 = [NSDictionary dictionaryWithDictionary:subArray[i]];
        NSDictionary* dic2 = [NSDictionary dictionaryWithDictionary:[dic1 valueForKey:@"subject"]];
        NSString* title = [dic2 valueForKey:@"title"];
        NSString* year = [dic2 valueForKey:@"year"];
        float mark = [[[dic2 valueForKey:@"rating"] valueForKey:@"average"] floatValue];
  //      NSLog(@"%f",mark);
        NSDictionary* img = [dic2 valueForKey:@"images"];
        
        
        HomeModal* mod = [[HomeModal alloc] initWithTitle:title AndYear:year andMark:mark andImage:img];
        [modalArray addObject:mod];
      //  NSLog(@"%@",modalArray);
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
